package com.android.thought.badboys.Common;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;

import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;

import com.android.thought.badboys.Action.IPhoneAccessAction;
import com.android.thought.badboys.Info.SelectionPhoneInfo;

public class PhoneGroupLoadTask extends AsyncTask<Void, ProgressDialog, SelectionPhoneInfo > {

	Context mContext;
	ProgressDialog mDlg;	

	IPhoneAccessAction mListener = null;
 
	public PhoneGroupLoadTask(Context context, boolean isPro, IPhoneAccessAction listener) {

		mContext = context;

		this.mListener = listener;
		
		if ( isPro ) {

			mDlg = new ProgressDialog(mContext);
			mDlg.setIndeterminate(true);
			mDlg.setCancelable(false);
			mDlg.setMessage("잠시만 기다려 주세요. ");
			mDlg.show();
		}
	}

	@Override
	protected SelectionPhoneInfo doInBackground(Void... params) {

		ObjectInputStream inputStream = null;
		final String fileName = mContext.getApplicationInfo().dataDir + "/phoneGroupInfo.txt";

		SelectionPhoneInfo returnInfo = null;

		try {
			inputStream = new ObjectInputStream(new FileInputStream(fileName));

			// int fileLen = inputStream.available();

			returnInfo = (SelectionPhoneInfo) inputStream.readObject();

		} catch (Exception e) {
			return null;
		} finally {
			try {
				if (inputStream != null) {
					inputStream.close();
				}

			} catch (IOException e) {
				e.printStackTrace();
			}
		}

		return returnInfo;
	}

	@Override
	protected void onPostExecute(SelectionPhoneInfo result) {
		super.onPostExecute(result);

		if (this.mListener != null)
			this.mListener.onLoadComplated(result);

		if ( mDlg != null)
			mDlg.dismiss();
	}

}
